﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class AssetMapper
    {
        internal static IAsset ToBusinessObject ( T_Asset entity )
        {
            IAsset obj = new Asset (
                entity.Asset_IDPK ,
                entity.Name_NVC ,
                entity.StatusID_FK ,
                entity.Source_NVC ,
                entity.CreationDate_DT ,
                entity.UpdatedDate_DT ,
                entity.CreatorID_NVC ,
                entity.LatestEditorID_NVC
                );

            if ( entity.T_Status != null ) obj.Status = StatusMapper.ToBusinessObject ( entity.T_Status );

            if ( entity.T_AssetContents != null ) obj.AssetContentList = AssetContentMapper.ToBusinessObjects ( entity.T_AssetContents );
            if ( entity.T_AssetMetaDatas != null ) obj.AssetMetaDataList = MetaDataMapper.ToBusinessObjects ( entity.T_AssetMetaDatas.Select(p=> new MetaDataMapper.MetadataPairing{MetaKey = p.T_MetaKey,MetaValue=p.T_MetaValue}).ToList() );
            if ( entity.T_AssetRightsUsages != null ) obj.AssetRightsUsageList = AssetRightsUsageMapper.ToBusinessObjects ( entity.T_AssetRightsUsages );
            if ( entity.T_RelatedAssets != null ) obj.RelatedAssetList = RelatedAssetMapper.ToBusinessObjects ( entity.T_RelatedAssets );
            if ( entity.T_AssociatedContents != null ) obj.AssociatedContentList = AssociatedContentMapper.ToBusinessObjects ( entity.T_AssociatedContents );

            return obj;
        }

        internal static T_Asset ToEntity ( IAsset v )
        {
            T_Asset entity = new T_Asset ( );

            entity.Asset_IDPK = v.ID;
            entity.Name_NVC = v.Name;
            entity.StatusID_FK = v.StatusID;
            entity.Source_NVC = v.Source;
            entity.CreationDate_DT = v.CreationDate;
            entity.UpdatedDate_DT = v.UpdatedDate;
            entity.CreatorID_NVC = v.CreatorID;
            entity.LatestEditorID_NVC = v.LatestEditorID;

            if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );

            if ( v.AssetContentList != null ) entity.T_AssetContents = ( System.Data.Linq.EntitySet<T_AssetContent> ) AssetContentMapper.ToEntities ( v.AssetContentList );
            if ( v.AssetMetaDataList != null ) entity.T_AssetMetaDatas = ( System.Data.Linq.EntitySet<T_AssetMetaData> ) AssetMetaDataMapper.ToEntities (v.ID, v.AssetMetaDataList );
            if ( v.AssetRightsUsageList != null ) entity.T_AssetRightsUsages = ( System.Data.Linq.EntitySet<T_AssetRightsUsage> ) AssetRightsUsageMapper.ToEntities ( v.AssetRightsUsageList );
            if ( v.AssociatedContentList != null ) entity.T_AssociatedContents = ( System.Data.Linq.EntitySet<T_AssociatedContent> ) AssociatedContentMapper.ToEntities ( v.AssociatedContentList );
            if ( v.RelatedAssetList != null ) entity.T_RelatedAssets = ( System.Data.Linq.EntitySet<T_RelatedAsset> ) RelatedAssetMapper.ToEntities ( v.RelatedAssetList );

            return entity;
        }

        internal static IList<IAsset> ToBusinessObjects ( IList<T_Asset> dalList )
        {
            return dalList.Select ( tmp => ToBusinessObject ( tmp ) ).ToList ( );
        }

        internal static IList<T_Asset> ToEntities ( IList<IAsset> entityList )
        {
            EntitySet<T_Asset> assetList = new EntitySet<T_Asset>(); 
            entityList.Select ( tmp => ToEntity ( tmp ) ).ToList ( ).ForEach(assetList.Add);
            return assetList;
        }
    }
}
